package org.liu.async.bean;

import org.liu.async.kit.AsyncConfig;
import org.liu.async.kit.DynamicProxy;
import org.springframework.beans.factory.FactoryBean;

import java.util.concurrent.TimeUnit;

/**
 * Created by liuj on 2015/12/30.
 */
public class ProxyFactoryBean implements FactoryBean{

    /**
     *被代理的类型
     */
    private Class<?> targetClass;
    /**
     * 异步执行的超时时间
     */
    private long timeout;
    /**
     * 超时时间的单位
     */
    private TimeUnit timeUnit;
    /**
     * 异步执行的线程池大小,核心大小
     */
    private int coreSize = 10;
    /**
     * 线程池队列大小
     */
    private int queueSize = 10;
    /**
     * 是否需要异步并行,如果为false,则不会生成线程池,也不会异步执行
     */
    private boolean async = true;

    @Override
    public Object getObject() throws Exception {
        return DynamicProxy.getProxy(targetClass, new AsyncConfig(timeout,timeUnit,coreSize,queueSize,async));
    }

    @Override
    public Class<?> getObjectType() {
        return this.targetClass;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    public void setTargetClass(Class<?> targetClass) {
        this.targetClass = targetClass;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public void setTimeUnit(TimeUnit timeUnit) {
        this.timeUnit = timeUnit;
    }

    public void setCoreSize(int coreSize) {
        this.coreSize = coreSize;
    }

    public void setQueueSize(int queueSize) {
        this.queueSize = queueSize;
    }

    public void setAsync(boolean async) {
        this.async = async;
    }
}
